package special_tree;

import code.CodeList;
import code.Opcode;

import compiler.CompilerException;

import lexer.Token;
import tree.DataType;
import tree.MyTree;
import tree.Tree;
import tree.Type;
import tree.TypingException;

public class ExplicitTypeCastTree extends MyTree {

	@Deprecated
	private Type toTyp;
	
	public ExplicitTypeCastTree(Token t, Type toTyp, Tree expr) {
		super(t, expr);
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#computeType()
	 */
	@Override
	@Deprecated
	public Type computeType() throws TypingException {
		Type fromTyp = getOperand(0).computeType();
		if (fromTyp.equals(new Type(DataType.BOOL, "")) || toTyp.equals((new Type(DataType.BOOL, "")))) {
			if (((fromTyp.equals(new Type(DataType.BOOL, "")) && toTyp.equals((new Type(DataType.INT, ""))))) ||
					(((fromTyp.equals(new Type(DataType.INT, "")) && toTyp.equals((new Type(DataType.BOOL, ""))))))) {
				return this.toTyp;
			}
			throw new TypingException(getToken(),"Cannot cast from or to bool");
		}
		return this.toTyp;
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#generateCode(code.CodeList)
	 */
	@Override
	@Deprecated
	public void generateCode(CodeList code) throws CompilerException {
		getOperand(0).generateCode(code);
		if (this.toTyp.equals(new Type(DataType.BOOL, ""))) {
			code.generateCode(Opcode.INTTOBOOL);
		}
	}

}
